﻿using System.Collections.Generic;
using Beisen.Data;
using fKanban.Model;
using fKanban.Utility.ObjectExtend;

namespace fKanban.Provider
{
    public class SprintTaskProvider
    {
        public int Add(SprintTaskEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            var identity = SafeProcedure.ExecuteScalar(db, "dbo.jTaskAdd", delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@GoalId", e.GoalId);
                parameters.AddWithValue("@SprintId", e.SprintId);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@DashboardId", e.DashboardId);

                parameters.AddWithValue("@Author", e.Author);
                parameters.AddWithValue("@Description", e.Description);
                parameters.AddWithValue("@Duration", e.Duration);
                parameters.AddWithValue("@Name", e.Name);

                parameters.AddWithValue("@Status", e.Status);
                parameters.AddWithValue("@eColor", e.eColor);
                parameters.AddWithValue("@eDegree", e.eDegree);
            });
            return identity.ToInt();
        }
        public int Remove(SprintTaskEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jTaskRemoveByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
            });
        }
        public int Update(SprintTaskEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jTaskUpdateByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@GoalId", e.GoalId);
                parameters.AddWithValue("@SprintId", e.SprintId);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@DashboardId", e.DashboardId);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Description", e.Description);
                parameters.AddWithValue("@Author", e.Author);
                parameters.AddWithValue("@Duration", e.Duration);
                parameters.AddWithValue("@Status", e.Status);
                parameters.AddWithValue("@eColor", e.eColor);
                parameters.AddWithValue("@eDegree", e.eDegree);
            });
        }
        public List<SprintTaskEntity> LoadData(SprintTaskEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<SprintTaskEntity>(db, "dbo.jTaskGetListByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@GoalId", e.GoalId);
                parameters.AddWithValue("@SprintId", e.SprintId);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@DashboardId", e.DashboardId);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Description", e.Description);
                parameters.AddWithValue("@Author", e.Author);
                parameters.AddWithValue("@Duration", e.Duration);
                parameters.AddWithValue("@Status", e.Status);

                parameters.AddWithValue("@eDegree", e.eDegree);
                parameters.AddWithValue("@eColor", e.eColor);
            },
            delegate(IRecord record, SprintTaskEntity objectInstance)
            {
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.GoalId = record.GetOrDefault<int>("GoalId", 0);
                objectInstance.SprintId = record.GetOrDefault<int>("SprintId", 0);
                objectInstance.UserStoryId = record.GetOrDefault<int>("UserStoryId", 0);
                objectInstance.DashboardId = record.GetOrDefault<int>("DashboardId", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", "untitled");
                objectInstance.Description = record.GetOrDefault<string>("Description", string.Empty);
                objectInstance.Author = record.GetOrDefault<string>("Author", string.Empty);
                objectInstance.Duration = record.GetOrDefault<int>("Duration", 0);
                objectInstance.Status = record.GetOrDefault<int>("Status", 0);

                objectInstance.eDegree = record.GetOrDefault<int>("eDegree", 0);
                objectInstance.eColor = record.GetOrDefault<int>("eColor", 0);
            });
        }
    }
}